<?php
namespace app\index\controller;

use \app\backend\model\Menu;
use app\backend\org\HeadTitle;
use \think\Db;
use app\backend\org\Crumbs;
use app\backend\org\Kv;

/**
 * 患者获益控制器
 */
class Benefit  extends \think\Controller
{
    //患者获益benefit页面接口
    public function index()
    {
        try {
            $menu_id = input('menu_id', '-1', 'htmlspecialchars');//第三个参数过滤
            $language = input("lang", "cn", "htmlspecialchars");
            //当前页数
            $page=input('page','1','htmlspecialchars');
            //每页显示条数
            $psize=input('psize','1','htmlspecialchars');
            if ($language == "cn") {
                $tab_name = "tab_name_cn";
                $list_title = "list_title_cn";
                $stage_title = "stage_title_cn";
                $content= "content_cn";
            } elseif ($language == "en") {
                $tab_name = "tab_name_en";
                $list_title = "list_title_en";
                $stage_title = "stage_title_en";
                $content= "content_en";
            }
            $data = Menu::All(["p_id" => ["exp", "<>0"]]);
            //调用headtitle查title内容
            $head_title = HeadTitle::title($data, $menu_id, $language, "menu");
            //echo "<pre>";
            // print_r($head_title);exit;
            //调用kv类查kv画面内容
            $menu = Kv::Kv($menu_id);
            //echo "<pre>";
            //print_r($menu);exit;

            //tab分类切换
            //$tab = Db::query("select tab_id,p_id,$tab_name,type_id from ls_tab where `p_id`={$menu[0]['menu_id']} and `delete_time` is null order by sort ASC ");
            $tab = Db::query("select b.tab_id,b.$tab_name as tab_name,b.tab_name_cn as tab_name_cn_if,b.type_id,t.tt_id from ls_tab b left join ls_tab_type t on b.tab_id=t.tab_id where b.p_id={$menu[0]['menu_id']} and b.delete_time is null order by b.sort ASC ");

            //stage表下面内容
            $stage = Db::query("select stage_id,type_id,p_id,$stage_title as stage_title,$content as content,is_enable from ls_stage where `delete_time` is null order by `manual_sort` ASC ");

            //非患者活动
            $list = Db::query("select list_id,p_id,type_id,is_recommend,$list_title as list_title,img_url,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time,is_enable from ls_list where `delete_time` is null order by manual_sort ASC");
            $tempss = [];// 非“患者活动”的子分类 list_id 的数组，取反才有效果

            //计算偏移量 分页 患者活动
            $offset = ($page - 1)*$psize;

            foreach ($tab as $k => $v) {
                //置顶那一条不分页
                if($v["tab_name_cn_if"]=="患者活动"){
                    $list1 = Db::query("select list_id,p_id,news_id,type_id,is_recommend,$list_title as list_title,img_url,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time,is_enable from ls_list where `delete_time` is null and `is_enable`=1 and `is_recommend`='1,{$v['tt_id']}' ");
                    //var_dump($list1);exit;
                    //查询出p_id=28 news_id包含32 的数据
                    $list_other = Db::query("select list_id,news_id,p_id,type_id,is_recommend,$list_title as list_title,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time,is_enable from ls_list where ((`is_recommend`='0,{$v['tt_id']}') or (news_id LIKE '%{$v['tab_id']}%')) and `delete_time` is null and `is_enable`=1 order by create_time DESC limit $offset,$psize ");
                    $list3 = array_merge($list1, $list_other);
                    //新闻类分页
                   // $list2 = Db::query("select list_id,p_id,type_id,is_recommend,$list_title as list_title,img_url,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time,is_enable from ls_list where `delete_time` is null and `is_enable`=1 and `is_recommend`='0,{$v['tt_id']}' order by manual_sort ASC limit $offset,$psize ");
                    //$list3 = array_merge($list1,$list2);
                    //计算总页数
                    $count = Db::query("select count(*) as total from ls_list where `is_recommend`='0,{$v['tt_id']}' and `delete_time` is null and `is_enable`=1");
                    // var_dump($count);exit;
                        $page_count = ceil($count[0]['total'] / $psize);
                    //var_dump($list3);exit;
                }

                foreach ($list as $a => $b) {
                    if ($v["tab_name_cn_if"] != '患者活动' && $b['p_id'] == $v['tab_id'] && !in_array($b['list_id'] , $tempss)) {//in_array用法，$tempss中查找$b['list_id']
                        array_push($tempss, $b['list_id']);//array_push() 函数向第一个参数的数组尾部添加一个或多个元素（入栈），然后返回新数组的长度
                    }
                    //文章内容追加stage
                    foreach($stage as $c => $d){
                        if($b["list_id"] == $d["p_id"] && in_array($d['p_id'], $tempss)){
                            if(isset($list[$a]["child"])){
                                $list[$a]["child"][] = $d;
                            }else{
                                $list[$a]["child"] = [$d];
                            }
                        }
                    }
                    //所有的tab加list
                    if(($b['is_enable']==0 && $b["type_id"]==2 ) || ($b['is_enable']==1 && $b["type_id"]==1)){
                        //echo 11 ;exit;
                        if ($v["tab_id"] == $b["p_id"]) {
                            if (isset($tab[$k]["child"])) {
                                if($v["tab_name_cn_if"]=="患者活动"){
                                    $tab[$k]["child"] = $list3;
                                }else{
                                    $tab[$k]["child"][] = $list[$a];
                                }

                            } else {
                                if($v["tab_name_cn_if"]=="患者活动"){
                                    $tab[$k]["child"] = $list3;
                                }else{
                                    $tab[$k]["child"] = [$list[$a]];
                                }

                            }
                        }
                    }
                }
            }

            //获取面包屑
            $crumbs = Crumbs::crumbs($data, $menu_id, $language, "menu");
            // echo "<pre>";
            //print_r($crumbs);exit;
            //判断记录存不存在
            if (empty($menu)) {
                return json([
                    'code' => 2555,
                    'message' => '不存在这个id',
                ]);
            } else {
                return json([
                    'code' => 200,
                    'head_title' => $head_title,
                    'KV' => $menu[0],
                    'crumbs' => $crumbs,
                    'tab' => $tab,
                    'page_count'=>$page_count,
                    "imsg" => "患者获益-benefit.html "
                ]);
            }
        } catch (\Exception $e) {
//			$this->_log2db('index::被 catch 了');
            return json([
                'code' => 2333,
                'imsg' => '被catch',
                'message' => '数据库查询失败 ' . $e->getMessage(),
            ]);
        }
    }

    //患者获益详情接口
    public function main(){
        try{
            $list_id = input('list_id','-1','htmlspecialchars');//第三个参数过滤
            $language = input("lang","cn","htmlspecialchars");
            if($language=="cn"){
                $stage_title = "stage_title_cn";
                $content = "content_cn";
            }elseif($language=="en"){
                $stage_title = "stage_title_en";
                $content = "content_en";
            }

            $tab_id =  Db::query("select p_id from ls_list where `list_id`={$list_id} and `delete_time` is null");
            $menu_id = Db::query("select p_id from ls_tab where `tab_id`={$tab_id[0]["p_id"]} and `delete_time` is null ");
            //print_r($menu_id);exit;
            //调用kv类查kv画面内容
            $menu=Kv::Kv($menu_id[0]["p_id"]);

            //获取stage段落并组合
            $stage = Db::query("select stage_id,type_id,$stage_title as stage_title,$content as content,DATE_FORMAT(news_time,'%Y-%m-%d') as news_time from ls_stage where `p_id`={$list_id} and `delete_time` is null order by `manual_sort` ASC ");
            if(empty($stage)){
                $stage[0]="";
            }
            //echo "<pre>";
            // print_r($stage);exit;
            $data = Menu::All(["p_id"=>["exp","<>0"]]);
            //调用headtitle查title内容
            $head_title = HeadTitle::title($data,$list_id,$language,"list");

            //获取面包屑
            $crumbs =  Crumbs::crumbs($data,$list_id,$language,"list");
            //exit;
            // echo "<pre>";
            // print_r($crumbs);exit;
            // return json($crumbs);exit;
            //判断记录存不存在
            if( empty($menu)){
                return json([
                    'code' => 2555,
                    'message' => '不存在这个id',
                ]);
            }else{
                return json([
                    'code' => 200,
                    'head_title'=>$head_title,
                    'KV'=>$menu[0],
                    'crumbs'=>$crumbs,
                    'stage'=>$stage[0],
                    "imsg" => "患者获益-benefit-news.html"
                ]);
            }
        }catch (\Exception $e){
//			$this->_log2db('index::被 catch 了');
            return json([
                'code'=>2333,
                'imsg'=>'被catch',
                'message'=>'数据库查询失败'.$e->getMessage(),
            ]);
        }
    }
}
